from django.shortcuts import render
from .models import Book  #导入模型
from django.http import HttpResponse
def index(request):
#1.添加
    # book = Book(name = '西游记',author = '吴承恩',price=200)
    # book.save()

# 2.查询
    #2.1通过id查找
    #pk是primary_key的缩写  也可以用id ,用pk好处: 不用更改主键名字,pk代表所有主键
    # books = Book.objects.get(pk = 1)
    # #print(books)  #结果:Book object (1)

    # #在models设置 str方法后:
    # print(books)       #结果:<Book:(西游记,吴承恩,200.0)>

    #2.2 通过其他字段查找
    # books = Book.objects.filter(name='西游记')  #拿叫西游记的全部数据,[列表形式]
    # <QuerySet [<Book: <Book:(西游记,吴承恩,200.0)>>]>

    # books = Book.objects.filter(name='西游记').first()  # 拿叫西游记的一条数据
    # <Book:(西游记,吴承恩,200.0)>
    # print(books)

#删除

    # book = Book.objects.get(pk=1)
    # book.delete()

#修改
    # book = Book.objects.get(pk=2)
    # book.price = 400  #.要修改的字段 = 值
    # book.save() #保存下
    return HttpResponse("添加成功")

